package com.liuwei.spring.cloud.junit.leetcode.list;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.List;

@Slf4j
public class Reverse {

    /**
     * 单向链表的反转
     * https://blog.csdn.net/WKX18330698534/article/details/132333696
     * @param args
     */
    public static void main(String[] args) {
        //1,3,5,7,9
        Node a1 = new Node(1);
        Node a3 = new Node(2);
        a1.setNext(a3);

        Node a5 = new Node(3);
        a3.setNext(a5);

        Node a7 = new Node(4);
        a5.setNext(a7);

        Node a9 = new Node(5);
        a7.setNext(a9);
        a9.setNext(null);
        print(a1);
        print(addFirst(a1));
//        print(reverseList(a1));
    }

    public static void print(Node head){
        Node current = head;
        log.info("Node Start");
        log.info("Node:{}",current.value);
        while(current.next!=null){
            log.info("Node:{}",current.next.value);
            current = current.next;
        }
        log.info("Node End");
    }

    /**
     * 头插法 values.add(0,x);
     * 原队列: 1->2->3->4->5
     * 仿制品: dummy
     * 第一步: 1->dummy
     * 第二步: 2->1->dummy
     * 第三步: 3->2->1->dummy
     * 第四步: 4->3->2->1->dummy
     * 第五步: 5->4->3->2->1->dummy
     */
    public static Node addFirst(Node head){
        if(head==null){
            return head;
        }
        Node dummy = null;
        Node current = head;
        while(current!=null){
            Node next = current.next;
            current.next = dummy;
            dummy = current;
            current = next;
        }
        return dummy;
    }

    @Data
    public static class Node{
        int value;
        Node next;

        Node(int value){
            this.value = value;
        }
    }

}
